using System;
using System.Collections;
#if UNITY_ANDROID
using Notifications.Runtime.Android;
#elif UNITY_IOS
using Notifications.Runtime.iOS;
#endif
using UnityEngine;

namespace Notifications.Runtime
{
    /// <summary>
    /// Global notifications manager that serves as a wrapper for multiple platforms' notification systems.
    /// </summary>
    public class GameNotificationsManager : MonoBehaviour
    {
        private static GameNotificationsManager _instance;
 
        private static readonly object _lock = new object();
        public static GameNotificationsManager Instance
        {
            get
            {
                lock (_lock)
                {
                    // _instance = FindObjectOfType(typeof(GameNotificationsManager)) as GameNotificationsManager;
                    if (_instance == null)
                    {
                        GameObject singletonGo = new GameObject("SingletonMono" + typeof(GameNotificationsManager).Name);
                        _instance = singletonGo.AddComponent<GameNotificationsManager>();
                        _instance.Awake();
                        DontDestroyOnLoad(singletonGo);
					
                    }
                    return _instance;
                }
            }
        }

        public static void Release()
        {
            if (_instance!= null)
            {
               Destroy(_instance.gameObject); 
            }
            _instance = null;
        }
        
        public IGameBackgroundNotificationsPlatform BackgroundNotificationsPlatform{ get; private set; }

        public bool Initialized { get; private set; }

        public static bool IsInstanceCreated => _instance != null;

        // Flag set when we're in the foreground
        private bool inForeground = true;
        
        
        private void Awake()
        {
            if (!Initialized)
            {
                _instance = this;
                _instance.Initialize();
            }
        }

        private void OnApplicationPause(bool pause)
        {
            if (BackgroundNotificationsPlatform == null || !Initialized)
            {
                return;
            }
            inForeground = !pause;
            if (inForeground)
            {
                OnForegrounding();
            }
            else
            {
                OnBackground();
            }
        }
        
        private  void OnDestroy()
        {
            inForeground = false;
            if (BackgroundNotificationsPlatform != null)
            {
                BackgroundNotificationsPlatform.Dispose();
            }
        }

        private void Initialize()
        {
            if (Initialized)
            {
                return;
            }

            Initialized = true;

#if UNITY_ANDROID
            BackgroundNotificationsPlatform = new AndroidBackgroundNotificationManager();
            BackgroundNotificationsPlatform.Init();
#elif UNITY_IOS
            BackgroundNotificationsPlatform = new iOSBackgroundNotificationManager();
            BackgroundNotificationsPlatform.Init();
#endif
            OnForegrounding();
        }
        public IGameBackgroundNotification CreateBackgroundNotification()
        {
            return BackgroundNotificationsPlatform?.CreateBackgroundNotification();
        }

        /// <summary>
        /// must called in main thread
        /// </summary>
        /// <param name="notification"></param>
        public void ScheduleBackgroundNotification(IGameBackgroundNotification notification)
        {
            if (notification == null || BackgroundNotificationsPlatform == null)
            {
                Debug.LogWarning("ScheduleNotification is not support on this platform:" + Application.platform.ToString());
                return;
            }
            BackgroundNotificationsPlatform.ScheduleBackgroundNotification(notification);
        }
        
        public void CancelBackgroundNotification(int id)
        {
            if (BackgroundNotificationsPlatform == null)
            {
                Debug.LogWarning("ScheduleNotification is not support on this platform:" + Application.platform.ToString());
                return;
            }
            BackgroundNotificationsPlatform.CancelBackgroundNotification(id);
        }

        public void CancelAllBackgroundNotifications()
        {
            if (BackgroundNotificationsPlatform == null)
            {
                Debug.LogWarning("ScheduleNotification is not support on this platform:" + Application.platform.ToString());
                return;
            }
            BackgroundNotificationsPlatform.CancelAllBackgroundNotifications();
        }
        
        
        /// <summary>
        /// update a exist background notification
        /// </summary>
        /// <param name="notification"></param>
        /// <exception cref="InvalidOperationException"></exception>
        public void UpdateBackgroundNotification(IGameBackgroundNotification notification)
        {
            if (notification == null)
            {
                Debug.LogWarning("ScheduleNotification is not support on this platform:" + Application.platform.ToString());
                return;
            }
            notification.NeedUpdate = true;
            BackgroundNotificationsPlatform.UpdateDateBackgroundNotification(notification);
        }

        private void OnForegrounding()
        {
            BackgroundNotificationsPlatform?.OnForeground();
        }

        private void OnBackground()
        {
            BackgroundNotificationsPlatform?.OnBackground();
        }
    }
}
